package com.youlai.boot.stepsPlan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youlai.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.stepsPlan.mapper.UserStepsPlanMapper;
import com.youlai.boot.stepsPlan.service.UserStepsPlanService;
import com.youlai.boot.stepsPlan.model.entity.UserStepsPlan;
import com.youlai.boot.stepsPlan.model.form.UserStepsPlanForm;
import com.youlai.boot.stepsPlan.model.query.UserStepsPlanQuery;
import com.youlai.boot.stepsPlan.model.vo.UserStepsPlanVO;
import com.youlai.boot.stepsPlan.converter.UserStepsPlanConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 用户每日步数计划服务实现类
 *
 * @author cuisheng
 * @since 2025-03-31 22:59
 */
@Service
@RequiredArgsConstructor
public class UserStepsPlanServiceImpl extends ServiceImpl<UserStepsPlanMapper, UserStepsPlan> implements UserStepsPlanService {

    private final UserStepsPlanConverter userStepsPlanConverter;

    /**
    * 获取用户每日步数计划分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<UserStepsPlanVO>} 用户每日步数计划分页列表
    */
    @Override
    public IPage<UserStepsPlanVO> getUserStepsPlanPage(UserStepsPlanQuery queryParams) {
        Page<UserStepsPlanVO> pageVO = this.baseMapper.getUserStepsPlanPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取用户每日步数计划表单数据
     *
     * @return
     */
    @Override
    public UserStepsPlanForm getUserStepsPlanFormData() {
        UserStepsPlan entity = this.getOne(new QueryWrapper<UserStepsPlan>().eq("user_id", SecurityUtils.getUserId()));
        return userStepsPlanConverter.toForm(entity);
    }
    
    /**
     * 新增用户每日步数计划
     *
     * @param formData 用户每日步数计划表单对象
     * @return
     */
    @Override
    public boolean saveUserStepsPlan(UserStepsPlanForm formData) {
        UserStepsPlan entity = userStepsPlanConverter.toEntity(formData);
        Integer userId = SecurityUtils.getUserId().intValue();
        entity.setUserId(userId);
        try {
            UserStepsPlan pre = getOne(new QueryWrapper<UserStepsPlan>().eq("user_id", userId));
            if (pre != null) {
                pre.setSteps(formData.getSteps());
                return this.updateById(pre);
            }
        }catch (Exception e){
            remove(new QueryWrapper<UserStepsPlan>().eq("user_id", userId));
        }
        entity.setSteps(formData.getSteps());
        return this.save(entity);
    }
    
    /**
     * 更新用户每日步数计划
     *
     * @param formData 用户每日步数计划表单对象
     * @return
     */
    @Override
    public boolean updateUserStepsPlan(UserStepsPlanForm formData) {
        formData.setUserId(SecurityUtils.getUserId().intValue());
        UserStepsPlan entity = userStepsPlanConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除用户每日步数计划
     *
     * @param ids 用户每日步数计划ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteUserStepsPlans(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的用户每日步数计划数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
